﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
///db_admin 的摘要说明
/// </summary>
public class db_admin
{
    SqlDBClass db;
	public db_admin()
	{
        db = new SqlDBClass();
	}

    /// <summary>
    /// 管理员登录
    /// </summary>
    /// <param name="username"></param>
    /// <param name="pwd"></param>
    /// <returns>0:用户名，1:最后登录时间，2:最后登录IP</returns>
    public string[] isLogin(string username, string pwd) {
        string[] info = new string[4];
        DataSet ds=checkPwdDS(username, pwd);
        if ( ds!= null)
        {
            info[0] = ds.Tables[0].Rows[0][0].ToString();
            info[1] = ds.Tables[0].Rows[0][2].ToString();
            info[2] = ds.Tables[0].Rows[0][3].ToString();
            //info[3] = ds.Tables[0].Rows[0][3].ToString();
          // info[4] = ds.Tables[0].Rows[0][4].ToString();
            if (updateTimeIP(username))
                return info;
            else
                return null;
        }
        else
        {
            return null;
        }
    }

    /// <summary>
    /// 插入当前登录时间和IP
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public Boolean updateTimeIP(string name) {
        string strsql = "update admin set lastIP='" + HttpContext.Current.Request.UserHostAddress + "',lastTime='" + DateTime.Now + "'where  adminname='" + name + "'";
        return db.execSqlNoQuery(strsql);
    }

    /// <summary>
    /// 检查密码是否正确
    /// </summary>
    /// <param name="username"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    public Boolean checkPwd(string username, string pwd) {
        string sql = "select * from admin where adminname='" + username + "' and adminpwd='" + pwd + "'";
        DataSet ds = new DataSet();
        ds = db.execSql(sql);
        if ((ds.Tables.Count != 0) && (ds.Tables[0].Rows.Count == 1))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public DataSet checkPwdDS(string username, string pwd)
    {
        string sql = "select * from admin where adminname='" + username + "' and adminpwd='" + pwd + "'";
        DataSet ds = new DataSet();
        ds = db.execSql(sql);
        if ((ds.Tables.Count != 0) && (ds.Tables[0].Rows.Count == 1))
        {
            return ds;
        }
        else
        {
            return null;
        }
    }


    /// <summary>
    /// 重设密码
    /// </summary>
    /// <param name="name"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    public Boolean setPwd(string name,string pwd){
        string strsql = "update admin set adminpwd='" + pwd + "'where  adminname='" + name + "'";
        return db.execSqlNoQuery(strsql);
    }

    /// <summary>
    /// 删除管理员用户
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public Boolean delAdmin(string name) {
        string strsql = "delete from admin where adminname='" + name + "'";
        return db.execSqlNoQuery(strsql);
    }

    /// <summary>
    /// 查询所有管理员
    /// </summary>
    /// <returns></returns>
    public DataSet selectAdminALL() {
        string strsql = "select * from admin order by id desc";
        return db.execSql(strsql);
    }

    /// <summary>
    /// 查询单个 管理员
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public DataSet selectAdminONE(string name) {
        string strsql = "select * from admin where adminname='" + name + "' order by id desc";
        return db.execSql(strsql);
    }

    public Boolean addAmin(string name,string pwd)
    {
        string strsql = "Insert Into admin(adminname,adminpwd) Values('" + name + "','" + pwd + "')";
        return db.execSqlNoQuery(strsql);
    }

}